package com.jivam.lottery.panel;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import com.jivam.lottery.data.DataCalculator;
import com.jivam.lottery.data.DataUtil;
import com.jivam.lottery.model.Num;
import com.jivam.lottery.util.Constants;
import com.jivam.lottery.util.ShowUtil;

/**
 * 期数统计面板
 * @author Jivam
 */
public class NumPanel extends AbstractPanel{

	private static final long serialVersionUID = 5007652692581410007L;

	private JPanel center;
	private JTextArea text = new JTextArea(Constants.KEY_NUM.length,46);
	private File f = new File(Constants.PATH_NOTE);
	public NumPanel() {
		super();
		init();
	}
	
	@Override
	protected void init() {
		this.setLayout(new BorderLayout());
		
		JPanel north = new JPanel();
		north.setLayout(new GridLayout(1,Constants.KEY_NUM.length*2));
		for(int i = 0; i < Constants.KEY_NUM.length*2 ; i++){
			if(i%2==0){
				north.add(new JLabel(Constants.KEY_NUM[i/2],SwingConstants.CENTER));
			}else{
				north.add(new JLabel(Constants.LABEL_PERIOD,SwingConstants.CENTER));
			}
		}
		center = new JPanel();
		center.setLayout(new GridLayout(10,Constants.KEY_NUM.length*2));
		try {
			reloadPanel();
		} catch (Exception e) {
			center.add(new JLabel("NumPanel 初始化错误"+e.getMessage()));
			DataUtil.writeLog(e);
		}
		
		JPanel south = new JPanel();
		south.setLayout(new GridLayout(1,1));
		text.setLineWrap(true);
		JScrollPane jsp = new JScrollPane(text);
		jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		readFile();
		south.add(jsp);
		
		this.add(north,BorderLayout.NORTH);
		this.add(center,BorderLayout.CENTER);
		this.add(south,BorderLayout.SOUTH);
	}
	

	@Override
	public void reloadPanel() {
		center.removeAll();
		for(int i = 0 ; i < 10 ; i++){
			for(List<Num> nums : DataCalculator.numls){
				addLabel(nums.get(i));
			}
		}
	}
	
	private void addLabel(Num num){
		JLabel[] ls = ShowUtil.getLabelListByNum(num);
		center.add(ls[0]);
		center.add(ls[1]);
	}
	
	/**
	 * 读取文件
	 */
	private void readFile(){
		FileReader fr = null;
		BufferedReader br=null;
		text.setText("");
		try {
			if(!f.exists()){
				f.createNewFile();
			}
			fr = new FileReader(f);
			br = new BufferedReader(fr);
			String str=null;
			while((str=br.readLine())!=null){
				text.append(str+"\n");
			}
		} catch (IOException e) {
			DataUtil.writeLog("期数统计面板的文本文件不存在", e);
		}finally{
			if(br!=null)try{br.close();}catch(IOException e){}
			if(fr!=null)try{fr.close();}catch(IOException e){}
		}
	}
	
	/**
	 * 期数文件保存
	 */
	public void saveFile(){
		FileWriter fw=null;
		try {
			fw = new FileWriter(f);
			fw.write(text.getText());
			fw.flush();
		} catch (IOException e) {
			DataUtil.writeLog("期数统计面板的文本文件保存失败", e);
		} finally{
			if(fw!=null)try {fw.close();} catch (IOException e) {}
		}
	}
	
}
